package com.gamex.findit.controller.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gamex.findit.ResultCode;
import com.gamex.findit.internal.DefaultConfig;
import com.gamex.findit.model.GameItem;
import com.gamex.findit.service.GameItemService;
import com.gamex.findit.service.MemberItemService;
import com.gamex.findit.service.MemberService;
import com.gamex.findit.service.NoticeService;
import com.gamex.findit.util.CommonUtil;

@Controller("memberGameItem")
public class GameItemController {
	
	private static final Logger logger = LoggerFactory.getLogger(GameItemController.class);
	
	@Autowired NoticeService noticeService;
	@Autowired GameItemService gameItemService;
	@Autowired MemberItemService memberItemService;
	@Autowired DefaultConfig config;
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param gameItem
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/app/member/itemList.*")
	public ResponseEntity<String> itemList( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, Model model
							, @ModelAttribute GameItem gameItem) throws Exception{
		
		HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "application/json; charset=utf-8");
        
		ModelMap body = new ModelMap();
		List<GameItem> result = null;
		try {
			
			result = gameItemService.getGameItem(gameItem);
		} catch (Exception e) {
			body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
		}
		
		if(result != null){
			body.addAttribute(ResultCode.RESULT,ResultCode.RESULT_CODE_SUCCESS);
			body.addAttribute("list", result);
		}else{
			body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
		}
		
	return new ResponseEntity<String>(CommonUtil.toJson(body), responseHeaders, HttpStatus.CREATED);
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param gameItem
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/app/member/item.*")
	public ResponseEntity<String> viewItem( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, ModelMap model
							, @ModelAttribute GameItem gameItem) throws Exception{
		
		HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "application/json; charset=utf-8");
        
		ModelMap body = new ModelMap();
		GameItem result = null;
		try {
			
			result = gameItemService.getGameItemView(gameItem.getItemNo());
		} catch (Exception e) {
			body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
		}
		
		if(result != null){
			body.addAttribute(ResultCode.RESULT,ResultCode.RESULT_CODE_SUCCESS);
			body.addAttribute("detail", result);
		}else{
			body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
		}
		
	return new ResponseEntity<String>(CommonUtil.toJson(body), responseHeaders, HttpStatus.CREATED);
	}
	

	
}
